package com.smh.server;

import com.smh.User;

import java.io.*;
import java.net.Socket;
import java.util.ArrayList;

public class ServerThread extends Thread {

    //创建Socket对象
    Socket socket;
    //创建集合对象
    static ArrayList<User> users;//所有进程公用一个
    static ArrayList<Socket> list;

    public ServerThread(Socket socket, ArrayList<User> users, ArrayList<Socket> list) {
        this.socket = socket;
        this.users = users;
        this.list = list;
    }

    @Override
    public void run() {
        //处理登录和注册请求
        while (true) {
            try {

                if (socket.isClosed()) {
                    list.remove(socket);
                    return;
                }
                BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                String flag = br.readLine();
                if (flag == null) {
                    continue;
                }
                String userInfo = br.readLine();
                if (userInfo == null) {
                    continue;
                }
                String[] split = userInfo.split("=|&");
                User user = new User(split[1], split[3]);
                //获取写回流
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));

                //判断操作
                if (flag.equals("1")) {
                    //登录操作
                    if (login(user, bw)) {
                        listening(br, user);
                    }
                } else if (flag.equals("2")) {
                    //注册操作
                    if (register(user, bw)) {
                        listening(br, user);
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            } finally {
            }
        }
    }

    private void listening(BufferedReader br, User user) throws IOException {
        //监听客户端发来的数据,再转发给所有的客户端
        while (true) {
            if (socket.isClosed()) {
                list.remove(socket);
                return;
            }
            String message = br.readLine();
            if (message != null) {
                for (Socket socket1 : list) {
                    BufferedWriter bw1 = new BufferedWriter(new OutputStreamWriter(socket1.getOutputStream()));
                    bw1.write(message + "\n");
                    bw1.flush();
                    //socket1.shutdownOutput();
                }
            }
        }
    }

    private boolean login(User user, BufferedWriter bw) throws IOException {
        //查询是否有用户以及密码是否正确
        int i = 0;
        for (User user1 : users) {
            if (user1.getUsername().equals(user.getUsername())) {
                i++;
                if (user1.getPassword().equals(user.getPassword())) {
                    bw.write("1" + "\n");
                    bw.flush();
                    //socket.shutdownOutput();
                    return true;

                } else {
                    bw.write("密码错误" + "\n");
                }
            }
        }
        if (i == 0) {
            bw.write("未查询到用户名,请先注册" + "\n");
        }
        bw.flush();
        //socket.shutdownOutput();//写结束标记
        return false;
    }

    private boolean register(User user, BufferedWriter bw) throws IOException {
        //查询用户名是否重复
        synchronized (ServerThread.class) {//保证文件同时只有一个进程在操作
            int i = 0;
            for (User user1 : users) {
                if (user1.getUsername().equals(user.getUsername())) {
                    i++;
                    bw.write("用户名已被注册" + "\n");
                    break;
                }
            }
            if (i != 0) {
                bw.flush();
                //socket.shutdownOutput();
                return false;
            }

            bw.write("2" + "\n");
            bw.flush();
            //socket.shutdownOutput();

            users.add(user);//将用户信息添加到集合中
            //更新本地文件
            File file = new File("sockethomework\\save\\UserInfo.txt");
            BufferedWriter bwInfo = new BufferedWriter(new FileWriter(file));
            for (User user1 : users) {
                bwInfo.write(user1.getUsername() + "=" + user1.getPassword());
                bwInfo.newLine();
            }
            //bw.flush();
            bwInfo.close();
            return true;
        }
    }
}
